var api = require("../../api.js"), app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        series_flag: true,

        tp_series: true,
        tp_model: true,
    },
    // 选择车系
    series: function(t) {
        let that = this;
        that.setData({
            tp_series: false
        })
        app.request({
            url: api.car.series,
            method: 'GET',
            success: function(res) {
                console.log(res)
                that.setData({
                    series: res
                })
            },
            fail: function(res) {},
            complete: function(res) {},
        })
    },

    detail_car: function(t) {
        console.log(t)
        console.log(t.currentTarget.dataset.car_name);
        let s_id = t.currentTarget.dataset.sid;
        let that = this;
        that.data.detail.series_name = t.currentTarget.dataset.car_name;
        let detail = that.data.detail;
        // 关闭车系选择  打开车型选择
        that.setData({
            tp_series: true,
            tp_model: false,
            detail: detail,
        })
        let keyword = t.currentTarget.dataset.car_name;

        app.request({
            url: api.car.model,
            data: {
                keyword: keyword
            },
            method: 'GET',
            success: function(res) {
                console.log(res)
                that.setData({
                    model: res[0]
                })
            },
        })
    },
    // 选择车型
    model_list: function(t) {
        console.log(t);
        console.log(t.currentTarget.dataset.series_id);
        let model_id = t.currentTarget.dataset.id;
        console.log(model_id + "model_id")
        let that = this;
        // 车型 id model_id
        that.data.detail.model_id = t.currentTarget.dataset.model_id;
        // 车系 id series_id
        let series_id = t.currentTarget.dataset.series_id;
        that.data.detail.series_id = t.currentTarget.dataset.series_id;
        // 车型 名字
        that.data.detail.model_name = t.currentTarget.dataset.model_name;
        let detail = that.data.detail;

        console.log(that.data.detail.model_id + '====选择完车型后的 model_id ')
        console.log(that.data.detail.series_id + '====选择完车型后的  series_id')

        console.log(that.data.detail.model_name)
        that.setData({
            detail: detail,
            model_id: model_id,
            series_id: series_id,
            
            tp_model:true
        })

    },






    // submit changed form
    form_submit: function(t) {
        console.log(t.detail.value)
        let form_value = t.detail.value;
        let that = this;
        let id = that.data.id;
        let series_id = that.data.series_id;
        let model_id = that.data.model_id;

        console.log(series_id)
        console.log(model_id)
        app.request({
          url: api.car.submit,
            data: {
                id: id,
                buy_time: form_value.buy_time,
                engine_num: form_value.engine_num,
                frame_num: form_value.frame_num,
                model_id: model_id,
                owner: form_value.owner,
                plate_num: form_value.plate_num,
                price: form_value.price,
                series_id: series_id,
                total_mileage: form_value.total_mileage,
            },
            method: "POST",
            success: function(res) {
                console.log(res)
                if (res.code == 0) {
                    wx.redirectTo({
                        url: '/pages/car_list/car_list',
                    })
                }
            },
            fail: function(res) {},
            complete: function(res) {},
        })
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        let that = this;
        console.log(options)

        // return;
        let id = options.id;
        let car_name = options.car_name;
        // let model_id = options.model_id;
        // let series_id = options.series_id;

        // console.log(model_id + "onload+++++++++++++++++")
        // console.log(series_id + "onload+++++++++++++++++")

        that.setData({
            car_name: car_name,
            id: id,
        })
        app.request({
          url: api.car.my_form,
            data: {
                id: id
            },
            method: 'GET',
            success: function(res) {
                console.log(res.data[0])
                let carbarn_list = res.data;
                that.setData({
                    carbarn_list: res.data,
                    detail: res.data[0],

                    series_id: res.data[0].series_id,
                    model_id: res.data[0].model_id,
                })

                app.request({
                    url: api.car.series,
                    method: 'GET',
                    success: function(res) {
                        console.log(res[0])
                        let car_lsit = res[0];
                        that.setData({
                            _list: res[0]
                        })
                       
                    },
                })

            },
        })
    },



    //choose series_car
    series_car: function(e) {



    },
   
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

        console.log(wx.getSystemInfoSync().windowHeight)
        let that = this;
        that.setData({
            winH: wx.getSystemInfoSync().windowHeight
        })
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    }
})